load("@fbcode_macros//build_defs:build_file_migration.bzl", "fbcode_target", "non_fbcode_target")
load("@fbcode_macros//build_defs:cpp_library.bzl", "cpp_library")
load("@fbsource//xplat/folly:defs.bzl", "folly_xplat_library")
load("@fbsource//xplat/pfh/triage_InfrastructureSupermoduleOptou:DEFS.bzl", "triage_InfrastructureSupermoduleOptou")

oncall("fbcode_entropy_wardens_folly")

# xplat build rules

non_fbcode_target(
    _kind = folly_xplat_library,
    name = "dwarf",
    srcs = [
        "Dwarf.cpp",
        "DwarfImpl.cpp",
        "DwarfLineNumberVM.cpp",
        "DwarfSection.cpp",
        "DwarfUtil.cpp",
    ],
    feature = triage_InfrastructureSupermoduleOptou,
    raw_headers = [
        "Dwarf.h",
        "DwarfImpl.h",
        "DwarfLineNumberVM.h",
        "DwarfSection.h",
        "DwarfUtil.h",
    ],
    deps = [
        "fbsource//xplat/folly/portability:sys_mman",
        "fbsource//xplat/folly/portability:unistd",
        ":elf",
        ":elf_cache",
        ":symbolized_frame",
        "//third-party/boost:boost",
        "//third-party/glog:glog",
        "//xplat/folly:function",
        "//xplat/folly:optional",
        "//xplat/folly:range",
        "//xplat/folly:scope_guard",
        "//xplat/folly/portability/provide:libdwarf",  # @manual
    ],
)

non_fbcode_target(
    _kind = folly_xplat_library,
    name = "elf",
    srcs = [
        "Elf.cpp",
    ],
    feature = triage_InfrastructureSupermoduleOptou,
    raw_headers = [
        "Elf.h",
        "Elf-inl.h",
    ],
    deps = [
        "fbsource//xplat/folly/portability:sys_mman",
        "fbsource//xplat/folly/portability:unistd",
        "//third-party/glog:glog",
        "//xplat/folly:conv",
        "//xplat/folly:exception",
        "//xplat/folly:likely",
        "//xplat/folly:range",
        "//xplat/folly:scope_guard",
        "//xplat/folly/lang:c_string",
        "//xplat/folly/lang:safe_assert",
    ],
    exported_deps = [
        "fbsource//xplat/folly/container:span",
        "fbsource//xplat/folly/portability:config",
    ],
)

non_fbcode_target(
    _kind = folly_xplat_library,
    name = "elf_cache",
    srcs = ["ElfCache.cpp"],
    feature = triage_InfrastructureSupermoduleOptou,
    raw_headers = [
        "ElfCache.h",
    ],
    exported_deps = [
        "fbsource//xplat/folly/hash:hash",
        "fbsource//xplat/folly/portability:sys_mman",
        ":elf",
        "//third-party/boost:boost",
        "//xplat/folly:conv",
        "//xplat/folly:optional",
        "//xplat/folly:scope_guard",
        "//xplat/folly/memory:reentrant_allocator",
    ],
)

non_fbcode_target(
    _kind = folly_xplat_library,
    name = "line_reader",
    srcs = [
        "LineReader.cpp",
    ],
    feature = triage_InfrastructureSupermoduleOptou,
    raw_headers = [
        "LineReader.h",
    ],
    deps = [
        "//xplat/folly:file_util",
        "//xplat/folly:range",
    ],
)

non_fbcode_target(
    _kind = folly_xplat_library,
    name = "stack_trace",
    srcs = [
        "StackTrace.cpp",
    ],
    feature = triage_InfrastructureSupermoduleOptou,
    raw_headers = [
        "StackTrace.h",
    ],
    deps = [
        "fbsource//xplat/folly/portability:config",
        "//xplat/folly:cpp_attributes",
        "//xplat/folly:portability",
        "//xplat/folly/tracing:async_stack",
    ],
    exported_deps = [
        "fbsource//xplat/folly/portability:libunwind",
        "fbsource//xplat/folly/portability:sys_types",
    ],
)

non_fbcode_target(
    _kind = folly_xplat_library,
    name = "symbolized_frame",
    srcs = [
        "SymbolizedFrame.cpp",
    ],
    feature = triage_InfrastructureSupermoduleOptou,
    raw_headers = [
        "SymbolizedFrame.h",
    ],
    deps = [
        ":elf",
        "//xplat/folly:range",
    ],
)

non_fbcode_target(
    _kind = folly_xplat_library,
    name = "symbolizer",
    srcs = [
        "SignalHandler.cpp",
        "SymbolizePrinter.cpp",
        "Symbolizer.cpp",
    ],
    feature = triage_InfrastructureSupermoduleOptou,
    raw_headers = [
        "SignalHandler.h",
        "SymbolizePrinter.h",
        "Symbolizer.h",
    ],
    deps = [
        "fbsource//xplat/folly/io:iobuf",
        "fbsource//xplat/folly/portability:sys_mman",
        "fbsource//xplat/folly/portability:sys_syscall",
        ":dwarf",
        ":elf_cache",
        ":line_reader",
        ":stack_trace",
        "//third-party/glog:glog",
        "//xplat/folly:fbstring",
        "//xplat/folly:file_util",
        "//xplat/folly:memory",
        "//xplat/folly:optional",
        "//xplat/folly:range",
        "//xplat/folly:scope_guard",
        "//xplat/folly:string",
        "//xplat/folly:synchronized",
        "//xplat/folly/container:evicting_cache_map",
        "//xplat/folly/container:f14_hash",
        "//xplat/folly/debugging/symbolizer/detail:debug",
        "//xplat/folly/lang:to_ascii",
        "//xplat/folly/memory:sanitize_address",
        "//xplat/folly/tracing:async_stack",
    ],
    exported_deps = [
        "fbsource//xplat/folly/portability:unistd",
    ],
)

# fbcode build rules

fbcode_target(
    _kind = cpp_library,
    name = "dwarf",
    srcs = [
        "Dwarf.cpp",
        "DwarfImpl.cpp",
        "DwarfLineNumberVM.cpp",
        "DwarfSection.cpp",
        "DwarfUtil.cpp",
    ],
    headers = [
        "Dwarf.h",
        "DwarfImpl.h",
        "DwarfLineNumberVM.h",
        "DwarfSection.h",
        "DwarfUtil.h",
    ],
    deps = [
        "//folly:optional",
        "//folly/lang:safe_assert",
        "//folly/portability:config",
        "//folly/portability:unistd",
    ],
    exported_deps = [
        ":elf",
        ":elf_cache",
        ":symbolized_frame",
        "//folly:function",
        "//folly:range",
        "//folly/portability/provide:libdwarf",  # @manual
    ],
)

fbcode_target(
    _kind = cpp_library,
    name = "elf",
    srcs = [
        "Elf.cpp",
    ],
    headers = [
        "Elf.h",
        "Elf-inl.h",
    ],
    deps = [
        "//folly:exception",
        "//folly:scope_guard",
        "//folly/lang:c_string",
        "//folly/portability:sys_mman",
        "//folly/portability:unistd",
    ],
    exported_deps = [
        "//folly:conv",
        "//folly:likely",
        "//folly:range",
        "//folly/container:span",
        "//folly/lang:safe_assert",
        "//folly/portability:config",
    ],
    external_deps = [
        "glog",
    ],
)

fbcode_target(
    _kind = cpp_library,
    name = "symbolized_frame",
    srcs = ["SymbolizedFrame.cpp"],
    headers = ["SymbolizedFrame.h"],
    exported_deps = [
        "//folly:range",
    ],
)

fbcode_target(
    _kind = cpp_library,
    name = "line_reader",
    srcs = ["LineReader.cpp"],
    headers = ["LineReader.h"],
    deps = [
        "//folly:file_util",
    ],
    exported_deps = [
        "//folly:range",
    ],
)

fbcode_target(
    _kind = cpp_library,
    name = "stack_trace",
    srcs = ["StackTrace.cpp"],
    headers = ["StackTrace.h"],
    deps = [
        "//folly:portability",
        "//folly/portability:config",
        "//folly/portability:libunwind",
        "//folly/tracing:async_stack",
    ],
    exported_deps = [
        "//folly/portability:sys_types",
    ],
)

fbcode_target(
    _kind = cpp_library,
    name = "elf_cache",
    srcs = [
        "ElfCache.cpp",
    ],
    headers = [
        "ElfCache.h",
    ],
    deps = [
        "//folly:scope_guard",
        "//folly/portability:sys_mman",
    ],
    exported_deps = [
        ":elf",
        "//folly:optional",
        "//folly:range",
        "//folly/hash:hash",
        "//folly/memory:reentrant_allocator",
        "//folly/portability:config",
    ],
    exported_external_deps = [
        "boost",
    ],
)

fbcode_target(
    _kind = cpp_library,
    name = "symbolize_printer",
    srcs = [
        "SymbolizePrinter.cpp",
    ],
    headers = [
        "SymbolizePrinter.h",
    ],
    deps = [
        "//folly:demangle",
        "//folly:file_util",
        "//folly:scope_guard",
        "//folly/io:iobuf",
        "//folly/lang:to_ascii",
    ],
    exported_deps = [
        ":symbolized_frame",
        "//folly:fbstring",
        "//folly:range",
    ],
)

fbcode_target(
    _kind = cpp_library,
    name = "symbolizer",
    srcs = [
        "Symbolizer.cpp",
    ],
    headers = [
        "Symbolizer.h",
    ],
    deps = [
        ":elf",
        "//folly:file_util",
        "//folly:memory",
        "//folly:scope_guard",
        "//folly:synchronized",
        "//folly/container:evicting_cache_map",
        "//folly/debugging/symbolizer/detail:debug",
        "//folly/lang:safe_assert",
        "//folly/lang:to_ascii",
        "//folly/memory:sanitize_address",
        "//folly/portability:sys_mman",
        "//folly/tracing:async_stack",
    ],
    exported_deps = [
        ":dwarf",
        ":elf_cache",
        ":stack_trace",
        ":symbolize_printer",
        ":symbolized_frame",
        "//folly:fbstring",
        "//folly:optional",
        "//folly:range",
        "//folly:string",
        "//folly/io:iobuf",
        "//folly/portability:config",
        "//folly/portability:unistd",
    ],
)

fbcode_target(
    _kind = cpp_library,
    name = "signal_handler",
    srcs = [
        "SignalHandler.cpp",
    ],
    headers = [
        "SignalHandler.h",
    ],
    deps = [
        ":symbolizer",
        "//folly:scope_guard",
        "//folly/lang:to_ascii",
        "//folly/portability:sys_syscall",
        "//folly/portability:unistd",
    ],
    external_deps = [
        "glog",
    ],
)
